//
//  YDYSearchView.m
//  DY_Bilibili
//
//  Created by 杨德勇 on 2016/10/7.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

#import "YDYSearchView.h"

@interface YDYSearchView()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITextField *searchTextF;
/** 历史搜索记录 */
@property (nonatomic, strong) NSMutableArray *historySearchArray;
/** 显示历史记录 */
@property (nonatomic, strong) UITableView *historyTableView;
@end

static CGFloat const cellHeight = 44;
static NSString * const ydy_searchPathComponent = @"history.plist";
static NSString * const ydy_searchCellIdentifier = @"searchCell";

@implementation YDYSearchView


#pragma mark -
#pragma mark -外部方法
+(YDYSearchView *)searchView{
    return [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil]lastObject];
    
}
#pragma mark -
-(void)awakeFromNib{
    [super awakeFromNib];
    
    
    
    self.historyTableView = [[UITableView alloc]initWithFrame:CGRectMake(10, 70, YDYScreenSize.width - 20,  cellHeight * (self.historySearchArray.count + 1) )];
    [self addSubview:self.historyTableView];
    self.historyTableView.bounces = NO;
    self.historyTableView.delegate = self;
    self.historyTableView.dataSource = self;
    self.historyTableView.sectionFooterHeight = 44;
    self.historyTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.historyTableView.backgroundColor = [UIColor clearColor];
    if (self.historySearchArray.count == 0) {
        self.historyTableView.hidden = YES;
    }
    
}



- (IBAction)back:(id)sender {
    [UIView animateWithDuration:0.25 animations:^{
        self.alpha = 0;
        [self.searchTextF resignFirstResponder];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
    
}
- (IBAction)search:(UIButton *)sender {
    if (self.searchTextF.text.length) {
        [self.historySearchArray addObject:self.searchTextF.text];
        
        NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:ydy_searchPathComponent];
        [self.historySearchArray writeToFile:path atomically:YES];
        [self removeFromSuperview];
        YDYLog(@"搜索%@",self.searchTextF.text);
        YDYLog(@"search.plist : %@",path);
    }
}
-(void)clearHistorySearch{
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:ydy_searchPathComponent];
    
    [self.historySearchArray removeAllObjects];
    [self.historySearchArray writeToFile:path atomically:YES];
    self.historyTableView.hidden = YES;
}

#pragma mark -
#pragma mark -tableview dataSource && delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.historySearchArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ydy_searchCellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ydy_searchCellIdentifier];
    }
    cell.imageView.image = [UIImage imageNamed:@"ic_search_history1"];
    cell.textLabel.text = self.historySearchArray[indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:13.0];
    cell.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.8];
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, YDYScreenSize.width - 20, 44)];
    btn.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.8];
    [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [btn setTitle:@"清空历史记录" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clearHistorySearch) forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString * searchtext = self.historySearchArray[indexPath.row];
    YDYLog(@"搜索%@",searchtext);
}

#pragma mark -
#pragma mark -lazy loading
-(NSMutableArray *)historySearchArray{
    if (_historySearchArray == nil) {
       
        NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:ydy_searchPathComponent];
        
        _historySearchArray = [NSMutableArray arrayWithContentsOfFile:path];
        if (_historySearchArray == nil) {
            _historySearchArray = [NSMutableArray array];
            
        }
        
    }
    return _historySearchArray;
}
@end
